import {
  Channel,
  UserChannelResponse,
  AllChannelsResponse,
  ArticlesResponse,
} from "@/types/data";
import { RootThunkAction } from "@/types/store";
import { http } from "../../utils/http";
import { differenceBy } from "lodash";
const CHANNEL_KEY = "geek-channels";
export const getUserChannel = (): RootThunkAction => {
  return async (dispatch, getState) => {
    const res = await http.get<AllChannelsResponse>("channels");
    const {
      login: { token },
      home: { userChannel },
    } = getState();
    let restChannels = differenceBy(res.data.data.channels, userChannel, "id");
    if (token) {
      // 登录
      const res = await http.get<UserChannelResponse>("/user/channels");
      const { channels } = res.data.data;
      restChannels = channels;
    } else {
      // 未登录
      const localChannels = JSON.parse(
        localStorage.getItem(CHANNEL_KEY) ?? "[]"
      ) as Channel[];

      if (localChannels.length > 0) {
        // 有
        // userChannel = localChannels;
      } else {
        // 没有
        const res = await http.get<UserChannelResponse>("/user/channels");
        const { channels } = res.data.data;
        localStorage.setItem(CHANNEL_KEY, JSON.stringify(channels));
        // userChannel = channels;
      }
    }
    dispatch({ type: "home/getUserChannel", payload: restChannels });
  };
};
export const getAllChannel = (): RootThunkAction => {
  return async (dispatch, getState) => {
    const res = await http.get<AllChannelsResponse>("channels");
    const {
      home: { userChannel },
    } = getState();
    const restChannels = differenceBy(
      res.data.data.channels,
      userChannel,
      "id"
    );

    dispatch({ type: "home/getAllChannel", payload: restChannels });
  };
};
export const getArticleList = (
  channel_id: number,
  timestamp: string
): RootThunkAction => {
  return async (dispatch) => {
    const res = await http.get<ArticlesResponse>("/articles", {
      params: {
        channel_id,
        timestamp,
      },
    });

    dispatch({
      type: "home/getChannelArticles",
      payload: {
        channelId: channel_id,
        data: res.data.data,
      },
    });
  };
};
export const delChannel = (channel: Channel): RootThunkAction => {
  return async (dispatch, getState) => {
    const {
      login: { token },
    } = getState();

    if (token) {
      // 已登录
      await http.delete(`/user/channels/${channel.id}`);
    } else {
      // 未登录
      const localChannels = JSON.parse(
        localStorage.getItem(CHANNEL_KEY) ?? "[]"
      ) as Channel[];

      const userChannel = localChannels.filter(
        (item) => item.id !== channel.id
      );
      localStorage.setItem(CHANNEL_KEY, JSON.stringify(userChannel));
    }
    dispatch({ type: "home/delChannel", payload: channel });
  };
};
export const addChannel = (
  channel?: any,
  id?: any,
  seq?: any
): RootThunkAction => {
  return async (dispatch, getState) => {
    const {
      login: { token },
    } = getState();
    let body = { channels: [{ id, seq }] };
    if (token) {
      // 登录
      await http.patch("/user/channels", body);
    } else {
      // 未登录
      const localChannels = JSON.parse(
        localStorage.getItem(CHANNEL_KEY) ?? "[]"
      ) as Channel[];
      const userChannel = [...localChannels, channel];
      localStorage.setItem(CHANNEL_KEY, JSON.stringify(userChannel));
    }
    dispatch({ type: "home/addChannel", payload: channel });
  };
};
